package com.yanqu.road.dataHelper.action.zhounianqing;

import com.yanqu.road.dataHelper.action.excel.ZhouNianQingRankData;
import com.yanqu.road.dataHelper.action.excel.ZhouNianRankLogic;
import com.yanqu.road.dataHelper.gameServer.GameServer;
import com.yanqu.road.dataHelper.manager.ServerMgr;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.entity.servercenter.ServerInfo;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

public class PatronsDataETLCaller implements Callable<List<ZhouNianQingRankData>> {

    private List<Integer> eventType;

    private int rankType;

    ServerInfo serverInfo;

    int size;

    public PatronsDataETLCaller(List<Integer> eventType, int rankType, ServerInfo serverInfo, int size) {
        this.eventType = eventType;
        this.rankType = rankType;
        this.serverInfo = serverInfo;
        this.size = size;
    }

    @Override
    public List<ZhouNianQingRankData> call() throws Exception {

        GameServer testGameServer = new GameServer(serverInfo);
        ZhouNianRankLogic resource = new ZhouNianRankLogic(testGameServer);

        List<ZhouNianQingRankData> serverRankList = new ArrayList<>();

        //门客冲榜
        //本服    1       4
        //跨服    12       3
        Map<Integer, ActivityInfo> activityInfoMap = new ServerMgr().getActivityInfoBy(1, 4, 12, 3, serverInfo.getServerId(), "2021-02-25 00:00:00", "2022-08-15 00:00:00");
        if(activityInfoMap.size() > 0){
            List<Integer> activityIdList = new ArrayList<>();
            for (int id : activityInfoMap.keySet()) {
                activityIdList.add(id);
            }
            serverRankList = resource.getServerRankListByActivityId(serverInfo.getServerId(), activityIdList, size, rankType);
        }

        //List<ZhouNianQingRankData> serverRankList = resource.getServerRankList(serverInfo.getServerId(), eventType, size, rankType);

        if(serverRankList.size() > 0) {
            //填充活动数据
            //resource.fillActivityInfo(serverRankList);
            for (ZhouNianQingRankData rankData : serverRankList) {
                if(activityInfoMap.containsKey(rankData.getActivityId())) {
                    rankData.setDataTime(activityInfoMap.get(rankData.getActivityId()).getEndTime());
                }
            }

        }else {
            System.out.println("区服没有门客冲榜：" + serverInfo.getServerId());
            return serverRankList;
        }

        //最强门客数据填充
        //门客总赚钱|最强门客赚钱|门客id|门客皮肤
        for (ZhouNianQingRankData rankData : serverRankList) {
            List<UserPatrons> patronsList = resource.getUserPatrons(rankData.getUserId());
            BigInteger total = new BigInteger("0");
            BigInteger maxAbility = new BigInteger("0");
            UserPatrons max = null;
            int patronsId = 0;
            for (UserPatrons patrons : patronsList) {
                total = total.add(patrons.getAbility());
                if(patrons.getAbility().compareTo(maxAbility) > 0){
                    patronsId = patrons.getPatronsId();
                    maxAbility = patrons.getAbility();
                    max = patrons;
                }
            }
            List<String> paramList = new ArrayList<>();
            paramList.add(total.toString());
            paramList.add(maxAbility.toString());
            paramList.add(""+ patronsId);
            paramList.add(""+ max.getSkinId());
            rankData.setOtherValue(StringUtils.listToString(paramList, ZhouNianQingRankData.OTHER_VALUE_SEPARATOR));
        }
        return serverRankList;
    }


}
